package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import zhang.lao.build.kit.LogKit;
import zhang.lao.dao.base.WnCompanyDao;
import zhang.lao.build.mybatis.jdbc.auto.model.WnCompany;
import zhang.lao.build.mybatis.jdbc.auto.model.WnCompanyExample;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.HttpResult;
import javax.annotation.Resource;
import java.util.List;
import zhang.lao.build.tool.UUIDTool;
/**
* 
* @author 
* @create 
**/
 @Service
public class WnCompanyService{
	@Resource
	private WnCompanyDao wnCompanyDao;
    private static final String color[] = {"#993333","#99CC66","#99CCCC","#0099CC","#9933FF"};
	public String add(){
		return "console/wnCompany/wnCompany_form";
	}

	public String edit(ModelMap modelMap,java.lang.String id){
			modelMap.put("wnCompany", wnCompanyDao.selectByPrimaryKey(id));
		return "console/wnCompany/wnCompany_form";
	}
    public String list(){
        return "console/wnCompany/wnCompany_table";
    }
	public String choose( ModelMap modelMap){
        List<WnCompany> wnCompanies = wnCompanyDao.selectByExample(null);
        StringBuffer stringBuffer = new StringBuffer();
        if(wnCompanies!=null){
            for (int i = 0; i < wnCompanies.size(); i++) {
                WnCompany wnCompany = wnCompanies.get(i);
                String c = color[i%color.length];
                stringBuffer.append("<a href=\"/console/wn_company/admin/choose/"+wnCompany.getId()+"\" class=\"box_block hvr-float-shadow\" style=\"background-color:"+c+";\">  <div class=\"w_center_icon\"></div><div class=\"w_center_title\">"+wnCompany.getName()+"</div> </a>");
            }
        }
        modelMap.put("companyHtml",stringBuffer.toString());
        return "console/wnCompany/choose";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		WnCompanyExample wnCompanyExample = new WnCompanyExample();
        ControllerQueryTool.setWnCompanyCriteria(bootGridReq.getQuery(),wnCompanyExample.createCriteria());
		List<WnCompany> wnCompanyList = wnCompanyDao.selectByExample(wnCompanyExample);
		return new BootStrapGridResp(page.getTotal(),wnCompanyList);
	}

	public HttpResult save(String formObjectJson){
		WnCompany wnCompany= JSON.parseObject(formObjectJson,WnCompany.class);
			java.lang.String id=wnCompany.getId();
		if (id!=null) {
			wnCompanyDao.updateByPrimaryKeySelective(wnCompany);
		}else{
			wnCompany.setId(UUIDTool.getUUID());
			wnCompanyDao.insertSelective(wnCompany);
		}
		return HttpResultUtil.buildSuccess();

	}

	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
		wnCompanyDao.deleteByPrimaryKey(java.lang.String.valueOf(id));
		}
		return HttpResultUtil.buildSuccess();
	}
}
